import Mock from 'mockjs'

const userMenu = [
    {
        path: '/',
        noShowingChildren: true,
        title: '首页',
        children: [
            {
                name: 'home',
                path: '',
                component: 'home'
            }
        ]
    },
    {
        name: 'report',
        title: '报告管理',
        path: '/report',
        children: [
            {
                name: 'report/list',
                title: '报告列表',
                path: '/report/list',
                component: 'report/list'
            }
        ]
    }
]

const adminMenu = [
    {
        name: 'system',
        title: '系统管理',
        path: '/system',
        component: null,
        children: [{
            name: 'system/user',
            title: '用户管理',
            path: '/system/user',
            component: 'system/user',
        }, {
            name: 'system/role',
            title: '角色管理',
            path: '/system/role',
            component: 'system/role'
        }]
    }
]

const adminUser = {
    username: 'admin',
    password: '123456',
    token: 'eyJhbGciOiJIUzI1NiIs',
    menus: userMenu.concat(adminMenu)
}

const normalUser = {
    username: 'normal',
    password: '123456',
    token: 'InR5cCI6IkpXVCJ9',
    menus: userMenu
}

Mock.mock('/api/user/login', 'post', function (req) {
    const {username} = JSON.parse(req.body)
    let flag = false
    let data = {}
    if (username === adminUser.username) {
        flag = true
        data.username = username
        data.token = adminUser.token
    } else if (username === normalUser.username) {
        flag = true
        data.username = username
        data.token = normalUser.token
    }
    if (!flag) {
        return {success: false, message: '用户名或密码错误'}
    } else {
        return {success: true, message: '登录成功', data: data}
    }
})

Mock.mock('/api/user/logout', 'post', function (req) {
    const {username} = JSON.parse(req.body)
    return {success: true, data: {username: username}}
})

Mock.mock(/\/api\/user\/info\/\w?/, 'get', function (req) {
    const urlParts = req.url.split('/');
    const username = urlParts[urlParts.length - 1];
    console.log('getUserInfo', username)

    if (username === adminUser.username) {
        return {success: true, data: adminUser}
    } else if (username === normalUser.username) {
        return {success: true, data: normalUser}
    } else {
        return {success: false, message: '用户不存在'}
    }
})
Mock.mock(/.*/, 'get', (options) => {
    console.log('Mock intercepted:', options)
    return {code: 200, message: 'fallback'}
})
